/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ucjava.chat.admanager;

import com.ucjava.chat.commserve.rmi.common.PrefStore;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Martin Manger
 *
 * modified by Jonathan Davis
 */
public class AdGui extends javax.swing.JFrame {

    private IAdvertising advertisingObj;
    

    /**
     * Creates new form AdGui
     */
    public AdGui() {

        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        AdGuiLabel = new javax.swing.JLabel();
        AdGuiBox = new javax.swing.JComboBox();
        AdGuiButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        AdGuiLabel.setText("How many ads do you want to buy?");

        AdGuiBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" }));
        AdGuiBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AdGuiBoxActionPerformed(evt);
            }
        });

        AdGuiButton.setText("Buy");
        AdGuiButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AdGuiButtonActionPerformed(evt);
            }
        });

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(AdGuiLabel)
                .addGap(18, 18, Short.MAX_VALUE)
                .addComponent(AdGuiBox, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(AdGuiButton, javax.swing.GroupLayout.DEFAULT_SIZE, 77, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cancelButton))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(AdGuiLabel)
                .addContainerGap())
            .addComponent(AdGuiBox, javax.swing.GroupLayout.Alignment.TRAILING)
            .addComponent(cancelButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(AdGuiButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void AdGuiButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AdGuiButtonActionPerformed
        try {
            // TODO add your handling code here:
            PrefStore prefStr = PrefStore.makePrefStore();
            String userIP = prefStr.getServerIP();
            Registry registry = LocateRegistry.getRegistry(userIP);
            //IAdvertising adManager;
            advertisingObj = (IAdvertising) registry.lookup("PurchaseAdvertisements");

            String AdNum = (String) AdGuiBox.getSelectedItem();
            int adnumint = Integer.parseInt(AdNum);
            if (adnumint > 0) {
                int i = 0;
                while (i < adnumint) {
                    //prompt that assign what users ads into the string declared above
                    String Advariable = JOptionPane.showInputDialog("What do you want this ad to say?");
                    try {

                        //enters the users add into the linkedlist
                        this.advertisingObj.sellAds(Advariable);
                    } catch (RemoteException ex) {
                        Logger.getLogger(AdGui.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    //increments
                    i = i + 1;
                }
                setVisible(false);
                int userPrompt = JOptionPane.showConfirmDialog(null, "Do you want to buy more ads?", "", JOptionPane.YES_NO_OPTION);
                if (userPrompt == JOptionPane.NO_OPTION) {
                    setVisible(false);
                } else {
                    setVisible(true);
                }
            } else {
                JOptionPane.showMessageDialog(this, "Zero is not an acceptable choice");
            }
        } catch (RemoteException ex) {
            Logger.getLogger(AdGui.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NotBoundException ex) {
            Logger.getLogger(AdGui.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_AdGuiButtonActionPerformed

    private void AdGuiBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AdGuiBoxActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_AdGuiBoxActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        // TODO add your handling code here:
        setVisible(false);
    }//GEN-LAST:event_cancelButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(AdGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(AdGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(AdGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(AdGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new AdGui().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox AdGuiBox;
    private javax.swing.JButton AdGuiButton;
    private javax.swing.JLabel AdGuiLabel;
    private javax.swing.JButton cancelButton;
    // End of variables declaration//GEN-END:variables
}
